<?php

namespace Acme\CommonBundle\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Acme\CommonBundle\Entity\ConstContestType;

/**
 * @Route("/contest")
 */
class ContestController extends Controller
{
	/**
	 * @Route("/js_contest_type_generator", name="AcmeCommonBundle_JsContestTypeGenerator")
	 * @Template()
	 */
	public function jsContestTypeGeneratorAction() {
		$em = $this->getDoctrine()->getEntityManager();
		$temp_data = $em->getRepository('AcmeCommonBundle:ConstContestType')->findAll();
		foreach ($temp_data as $v) {
			if ($v instanceof ConstContestType) {
				if ($v->getLevel() == 0) {
					foreach ($v->getChildren() as $v1) {
						$children[] = array('id' => $v1->getId(), 'name' => $v1->getName());
					}
					$data[] = array('id' => $v->getId(), 'name' => $v->getName(), 'children' => $children);
					unset($children);
				}
			}
			
		}
		
		//echo "<pre>";
		//print_r($data);
		//return ;
		
		return array('data' => json_encode($data));
	}
}
